package com.kgc.storeconsumer.controller;

import com.kgc.basic.util.ResponseMsg;
import com.kgc.store.pojo.ProCombine;
import com.kgc.store.remote.ProCombineRemote;
import com.kgc.storeconsumer.feign.ProCombineFeign;
import com.kgc.storeconsumer.feign.ProCombineWorkFlowFeign;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@CrossOrigin("*")
@RequestMapping("/proCombine")
public class ProCombineController {

    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private ProCombineFeign proCombineFeign;
    @Autowired
    private ProCombineWorkFlowFeign proCombineWorkFlowFeign;

    @RequestMapping("getOneProCombine")
    @ResponseBody
    public ResponseMsg getOne(@RequestParam("code") String code){
        logger.info("ProCombineController getOne code:"+code);
        ResponseMsg responseMsg =proCombineFeign.getOne(code);
        logger.debug("proCombineFeign getOne responseMsg:"+responseMsg+",code:"+code);
        return responseMsg;
    }

    @RequestMapping("getProCombinePage")
    @ResponseBody
    public ResponseMsg getPage(String pageNow, String pageSize, String code, String status, String startDate,String endDate){
        logger.info("ProCombineController getPage pageNow:"+pageNow+",pageSize:"+pageSize+",code:"+code+",status:"+status);
        Map<String,Object> map = new HashMap<>();
        map.put("code",code);
        map.put("status",status);
        map.put("startDate",startDate);
        map.put("endDate",endDate);
        map.put("pageNow",pageNow);
        map.put("pageSize",pageSize);
        ResponseMsg responseMsg =proCombineFeign.getPage(map);
        logger.debug("proCombineFeign getPage responseMsg:"+responseMsg+",map:"+map);
        return responseMsg;
    }

    @RequestMapping("addProCombine")
    @ResponseBody
    public ResponseMsg add(@RequestBody ProCombine proCombine) {
        logger.info("ProCombineController add proCombine:"+proCombine);
        Map<String,Object> variables = new HashMap<>();
        variables.put("code",proCombine.getCode());//单据编号
        variables.put("type",proCombine.getTypeId());//流程类型
        proCombineWorkFlowFeign.startProcess(variables);
        ResponseMsg responseMsg = proCombineFeign.add(proCombine);
        logger.debug("proCombineFeign add responseMsg:"+responseMsg+",proCombine:"+proCombine);
        return responseMsg;
    }
    @RequestMapping("updateProCombine")
    @ResponseBody
    public ResponseMsg update(@RequestBody ProCombine proCombine) {
        logger.info("ProCombineController update proCombine:"+proCombine);
        ResponseMsg responseMsg =proCombineFeign.update(proCombine);
        logger.debug("proCombineFeign update responseMsg:"+responseMsg+",proCombine:"+proCombine);
        return responseMsg;
    }
    @RequestMapping("delProCombine")
    @ResponseBody
    public ResponseMsg del(@RequestParam("code") String code) {
        logger.info("ProCombineController del code:"+code);
        ResponseMsg responseMsg =proCombineFeign.del(code);
        logger.debug("proCombineFeign del responseMsg:"+responseMsg+",code:"+code);
        return responseMsg;
    }
    @RequestMapping("changProCombineStatus")
    @ResponseBody
    public ResponseMsg changStatus(@RequestParam("code") String code,@RequestParam("status") String status){
        logger.info("ProCombineController changStatus code:"+code+",status:"+status);
        ResponseMsg responseMsg =proCombineFeign.changStatus(code,status);
        logger.debug("proCombineFeign changStatus responseMsg:"+responseMsg+",status:"+status);
        return responseMsg;
    }

    @RequestMapping("/check")
    @ResponseBody
    public  ResponseMsg check(@RequestBody Map<String,Object> map){
        ResponseMsg responseMsg = new ResponseMsg();
        logger.info("SalesOrdersController check params,{}",map.toString());
        Map<String,Object> variables = new HashMap<>();
        variables.put("code",(String) map.get("code"));//单据编号
        variables.put("checkManId",(Integer) map.get("checkManId"));//审批人
        variables.put("checkManName",(String) map.get("checkManName"));//审批人
        variables.put("checkComment",(String) map.get("checkComment"));//审批意见
        variables.put("checkDate",new Date());//审批时间
        variables.put("checkRes",(boolean)map.get("checkRes"));//审批结果
        com.kgc.activiti.util.ResponseMsg msg=  proCombineWorkFlowFeign.competeTask(variables);
        responseMsg.setCode(msg.getCode());
        responseMsg.setMsg(msg.getMsg());
        return responseMsg;
    }
}
